package ubadb.core.components.bufferManager.bufferPool.replacementStrategies.lastUseDate;

import java.util.Comparator;

import ubadb.core.common.Page;
import ubadb.core.components.bufferManager.bufferPool.BufferFrame;
import ubadb.core.components.bufferManager.bufferPool.replacementStrategies.AbstractPageReplacementStrategy;
import ubadb.core.components.bufferManager.bufferPool.replacementStrategies.lastUseDate.LastUseDateBufferFrame;

public class LRUReplacementStrategy extends AbstractPageReplacementStrategy {
	
	public LRUReplacementStrategy()
	{
		super(
				new Comparator<BufferFrame>()
				{
					@Override
					public int compare(BufferFrame bf1, BufferFrame bf2) {
						// safe casts as we know all frames are of this type
						LastUseDateBufferFrame fbf1 = (LastUseDateBufferFrame) bf1; 
						LastUseDateBufferFrame fbf2 = (LastUseDateBufferFrame) bf2;
						
						return fbf2.getLastUseDate().compareTo(fbf1.getLastUseDate());
					}
				}
			);
	}
	
	@Override
	public BufferFrame createNewFrame(Page page)
	{
		return new LastUseDateBufferFrame(page);
	}

}
